package com.qixiaobao.common.core.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMqConfig
 * @Author yangxm
 * @Description rabbitmq配置类
 * @Date 2022/12/2 上午11:33
 */
@Configuration
public class RabbitMqConfig {

    /**
     * 直连交换机
     */
    public static final String DIRECT_EXCHANGE = "direct_exchange";

    /**
     * 凭证下载队列
     */
    public static final String PROOF_DIRECT_QUEUE = "proof_direct_queue";
    /**
     * 凭证路由键
     */
    public static final String PROOF_ROUTING_KEY = "proof_routing_key";

    /**
     * 批单下载队列
     */
    public static final String ENDORSES_DIRECT_QUEUE = "endorses_direct_queue";
    /**
     * 批单下载路由键
     */
    public static final String ENDORSES_ROUTING_KEY = "endorses_routing_key";

    /**
     * 银行回单下载队列PaymentProof
     */
    public static final String PAYMENT_DIRECT_QUEUE = "payment_direct_queue";
    /**
     * 银行回单下载路由键
     */
    public static final String PAYMENT_ROUTING_KEY = "payment_routing_key";

    /**
     * 发票下载队列
     */
    public static final String INVOICE_DIRECT_QUEUE = "invoice_direct_queue";
    /**
     * 发票下载路由键
     */
    public static final String INVOICE_ROUTING_KEY = "invoice_routing_key";
    /**
     * 保单文件下载队列
     */
    public static final String INSURE_FILE_DIRECT_QUEUE = "insure_file_direct_queue";
    /**
     * 保单文件下载路由键
     */
    public static final String INSURE_FILE_ROUTING_KEY = "insure_file_routing_key";

    /**
     * 保单文件上传队列
     */
    public static final String INSURE_FILE_UPLOAD_DIRECT_QUEUE = "insure_file_upload_direct_queue";
    /**
     * 保单文件上传路由键
     */
    public static final String INSURE_FILE_UPLOAD_ROUTING_KEY = "insure_file_upload_routing_key";


    /**
     * @Description 创建保单主题交换机
     * @Author yangxm
     * @Date 2022/12/2 下午1:43
     * @Param []
     */
    @Bean("directExchange")
    public DirectExchange directExchange(){
        /**
         * 创建交换器，参数说明：
         * String name：交换器名称
         * boolean durable：设置是否持久化，默认是 false。durable 设置为 true 表示持久化，反之是非持久化。
         * 持久化可以将交换器存盘，在服务器重启的时候不会丢失相关信息。
         * boolean autoDelete：设置是否自动删除，为 true 则设置队列为自动删除，
         */
        return new DirectExchange(DIRECT_EXCHANGE, true, false);
    }

    /**
     * @Description 创建保单凭证队列
     * @Author yangxm
     * @Date 2022/12/2 下午1:48
     * @Param []
     */
    @Bean("proofDirectQueue")
    public Queue proofDirectQueue(){
        /**
         * 创建队列，参数说明：
         * String name：队列名称。
         * boolean durable：设置是否持久化，默认是 false。durable 设置为 true 表示持久化，反之是非持久化。
         * 持久化的队列会存盘，在服务器重启的时候不会丢失相关信息。
         * boolean exclusive：设置是否排他，默认也是 false。为 true 则设置队列为排他。
         * boolean autoDelete：设置是否自动删除，为 true 则设置队列为自动删除，
         * 当没有生产者或者消费者使用此队列，该队列会自动删除。
         * Map<String, Object> arguments：设置队列的其他一些参数。
         */
        return new Queue(PROOF_DIRECT_QUEUE, true, false, false, null);
    }

    /**
     * @Description 保单凭证队列与交换机绑定
     * @Author yangxm
     * @Date 2022/12/2 下午1:51
     * @Param [insureTopicExchange, insureCredentialsQueue]
     */
    @Bean
    public Binding bindingProofQueue(@Qualifier("proofDirectQueue") Queue proofDirectQueue, @Qualifier("directExchange") DirectExchange directExchange){
        //将队列和交换机绑定, 并设置用于匹配键：routingKey路由键
        return BindingBuilder.bind(proofDirectQueue).to(directExchange).with(PROOF_ROUTING_KEY);
    }

    /**
     * @Description 创建批单下载队列
     * @Author yangxm
     * @Date 2022/12/15 11:10 AM
     * @Param []
     */
    @Bean("endorsesDirectQueue")
    public Queue endorsesDirectQueue(){
        return new Queue(ENDORSES_DIRECT_QUEUE, true, false, false, null);
    }

    /**
     * @Description 批单下载队列与交换机绑定
     * @Author yangxm
     * @Date 2022/12/15 11:11 AM
     * @Param [endorsesDirectQueue, directExchange]
     */
    public Binding bindingEndorsesQueue(@Qualifier("endorsesDirectQueue") Queue endorsesDirectQueue, @Qualifier("directExchange") DirectExchange directExchange){
        //将队列和交换机绑定, 并设置用于匹配键：routingKey路由键
        return BindingBuilder.bind(endorsesDirectQueue).to(directExchange).with(ENDORSES_ROUTING_KEY);
    }

    /**
     * @Description 创建银行回单下载队列
     * @Author yangxm
     * @Date 2024/11/20 15:52
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("paymentDirectQueue")
    public Queue paymentDirectQueue(){
        return new Queue(PAYMENT_DIRECT_QUEUE, true, false, false, null);
    }

    /**
     * @Description 银行回单下载队列与交换机绑定
     * @Author yangxm
     * @Date 2024/11/20 15:53
     * @param paymentDirectQueue
     * @param directExchange
     * @return org.springframework.amqp.core.Binding
     */
    public Binding bindingPaymentQueue(@Qualifier("paymentDirectQueue") Queue paymentDirectQueue, @Qualifier("directExchange") DirectExchange directExchange){
        //将队列和交换机绑定, 并设置用于匹配键：routingKey路由键
        return BindingBuilder.bind(paymentDirectQueue).to(directExchange).with(PAYMENT_ROUTING_KEY);
    }

    /**
     * 创建发票下载队列
     */
    @Bean("invoiceDirectQueue")
    public Queue invoiceDirectQueue(){
        return new Queue(INVOICE_DIRECT_QUEUE, true, false, false, null);
    }

    /**
     * 发票下载队列与交换机绑定
     */
    public Binding bindingInvoiceQueue(@Qualifier("invoiceDirectQueue") Queue invoiceDirectQueue, @Qualifier("directExchange") DirectExchange directExchange){
        //将队列和交换机绑定, 并设置用于匹配键：routingKey路由键
        return BindingBuilder.bind(invoiceDirectQueue).to(directExchange).with(INVOICE_ROUTING_KEY);
    }

    /**
     * 创建保单文件下载队列
     */
    @Bean("insureFileDirectQueue")
    public Queue insureFileDirectQueue(){
        return new Queue(INSURE_FILE_DIRECT_QUEUE, true, false, false, null);
    }

    /**
     * 保单文件下载队列与交换机绑定
     */
    public Binding bindingInsureFileQueue(@Qualifier("insureFileDirectQueue") Queue insureFileDirectQueue, @Qualifier("directExchange") DirectExchange directExchange){
        //将队列和交换机绑定, 并设置用于匹配键：routingKey路由键
        return BindingBuilder.bind(insureFileDirectQueue).to(directExchange).with(INSURE_FILE_ROUTING_KEY);
    }

    /**
     * 创建保单文件上传队列
     */
    @Bean("insureFileUploadDirectQueue")
    public Queue insureFileUploadDirectQueue(){
        return new Queue(INSURE_FILE_UPLOAD_DIRECT_QUEUE, true, false, false, null);
    }

    /**
     * 保单文件上传队列与交换机绑定
     */
    public Binding bindingInsureFileUploadQueue(@Qualifier("insureFileUploadDirectQueue") Queue insureFileUploadDirectQueue, @Qualifier("directExchange") DirectExchange directExchange){
        //将队列和交换机绑定, 并设置用于匹配键：routingKey路由键
        return BindingBuilder.bind(insureFileUploadDirectQueue).to(directExchange).with(INSURE_FILE_UPLOAD_ROUTING_KEY);
    }

    /**
     * Json转换器
     */
    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
